/*
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2007 Intel Corporation. All Rights Reserved.
//
*/
#include "umc_defs.h"
#if defined (UMC_ENABLE_DV_VIDEO_ENCODER)

#include "umc_dv_enc_compressor_def.h"
#include "umc_dv_enc_segment_compressor.h"
#include "umc_dv_enc_block.h"
#include "umc_dv_enc_encode_bit_stream.h"

namespace UMC
{

enum
{
    MBLOCK_LIMIT    = 608,  // (14*4*8 + 10*2*8)
    SEGMENT_LIMIT   = 3040  // (14*4*8 + 10*2*8) * 5
};

static
Ipp32s ComputeSegmentSize(V_SEGMENT *lpVSegment, Ipp32u qno)
{
    Ipp32s SegmentSize = 0, DCTBlockNum;
    for(DCTBlockNum = 0; DCTBlockNum < 5*6; DCTBlockNum++)
        SegmentSize += GetEncodedBlockSize(lpVSegment->m_pDCTBlocks + DCTBlockNum, qno);

    return SegmentSize;
}


void SegmentCompressor::QuantizeData(DV_SEGMENT *lpDVSegment)
{
    Ipp8u nCur = m_bQuantPredictor;
    Ipp32s DCTBlockNum;

    if (ComputeSegmentSize(&m_VSegment, nCur) <= SEGMENT_LIMIT)
    {
        while ((15 >= nCur + 1) && (ComputeSegmentSize(&m_VSegment, nCur + 1) <= SEGMENT_LIMIT))
        {
            nCur++;
        }
    }
    else if(nCur > 0)
    {
        do
        {
            nCur--;
        }
        while ((0 < nCur) && (ComputeSegmentSize(&m_VSegment, nCur) > SEGMENT_LIMIT));
    }

    lpDVSegment->m_EncodedMacroBlocks[0].staqno =
    lpDVSegment->m_EncodedMacroBlocks[1].staqno =
    lpDVSegment->m_EncodedMacroBlocks[2].staqno =
    lpDVSegment->m_EncodedMacroBlocks[3].staqno =
    lpDVSegment->m_EncodedMacroBlocks[4].staqno = nCur;

    //Do quantization
    for(DCTBlockNum=0; DCTBlockNum<5*6; DCTBlockNum++)
        QuantizeBlock(m_VSegment.m_pDCTBlocks+DCTBlockNum, nCur);

   m_bQuantPredictor = nCur;

} //void SegmentCompressor::PreQuantizeData()

}//namespace UMC

#endif //(UMC_ENABLE_DV_VIDEO_ENCODER)
